import requests,re,time
''''''
def change_name(name):
    new_name=re.sub(r'[\/\\\:\*\?\"\<\>\|]','_',name)
    return new_name
''''''
# 定义一个函数，用来获取网页的html内容

headers={'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'}


def get_html(url):
    response = requests.get(url,headers=headers)
    code =response.status_code
    if code == 200:
        html = response.text
        #print(html)
        print(f'{url}---测试成功')
    else:
        print(f'{url}---测试失败')
    return html
def get_movie_info(html):
    # 编写正则表达式来匹配标签中的中文电影标题,评分,以及链接
    title_pattern = r'<span class="title">([\u4e00-\u9fa5]+)</span>'
    score_pattern = r'<span class="rating_num" property="v:average">(.+?)</span>'
    quote_pattern = r'<span class="inq">(.*?)</span>'
    # 使用re.findall()函数来匹配所有符合pattern的字符串
    titles = re.findall(title_pattern,html)
    #print(titles)
    scores = re.findall(score_pattern,html)
    #print(scores)
    quotes = re.findall(quote_pattern,html)
    #print(quotes)
    # 将电影标题,评分,短评打包成一个元组列表
    zip_list = zip(titles,scores,quotes)
    return zip_list
def save_movie_info(zip_list):
    # 保存电影信息到csv文件
    for title,score,quote in zip_list:
        with open('douban.csv','a',encoding='utf-8') as f:
            f.write(title+'\t'+score+'\t'+quote+'\n')

if __name__ == '__main__':
    with open('douban.csv','w',encoding='utf-8') as f:   # 打开文件
        f.write("电影名称\t"+"评分\t"+"短评\n")
    for i in range(0,2):
        url = f'https://movie.douban.com/top250?start={i*25}&filter='
        html = get_html(url)
        zip_list = get_movie_info(html)
        save_movie_info(zip_list)
        print(f'第{i+1}页数据保存成功')
        time.sleep(2)






